package org.example.springboot.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xxl.job.core.biz.model.XxlJobInfo;
import lombok.AllArgsConstructor;
import org.example.springboot.common.config.XxlJobConfig;
import org.example.springboot.common.service.impl.BaseServiceImpl;
import org.example.springboot.common.utils.XxlJobUtil;
import org.example.springboot.convert.CycleConvert;
import org.example.springboot.dao.CycleDao;
import org.example.springboot.entity.Cycle;
import org.example.springboot.entity.DecisionTime;
import org.example.springboot.entity.FmcMetrics;
import org.example.springboot.service.CycleService;
import org.example.springboot.service.DecisionTimeService;
import org.example.springboot.service.FmcMetricsService;
import org.example.springboot.utils.CronExpressionConverter;
import org.example.springboot.vo.CycleVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * 
 *
 * @author Fz
 * @since 1.0.0 2024-07-31
 */
@Service
@AllArgsConstructor
public class CycleServiceImpl extends BaseServiceImpl<CycleDao, Cycle> implements CycleService {

    @Resource
    private FmcMetricsService fmcMetricsService;

    @Resource
    private final DecisionTimeService decisionTimeService;

    @Resource
    private final XxlJobConfig xxlJobConfig;


    @Override
    public boolean doSaveOrUpdate(JSONObject irrigationTypeMsg, String uuid) {
        CycleVO cycleVO = JSON.parseObject(JSON.toJSONString(irrigationTypeMsg), CycleVO.class);
        Cycle cycle = CycleConvert.INSTANCE.convert(cycleVO);
        cycle.setIrrigationUnitUuid(uuid);
        return this.saveOrUp(cycle);
    }

    @Override
    public void setCycleCheck(String controlDevice, String cycle) {
        String[] cycleArr = cycle.split(",");
        FmcMetrics metrics = fmcMetricsService.getOne(new QueryWrapper<FmcMetrics>().eq("did", controlDevice));
        if (null == metrics) {
            metrics = new FmcMetrics();
            metrics.setDid(controlDevice);
            metrics.setEt((float) 0);
            metrics.setIrrigationAmount((float) 0);
            metrics.setFlowRem((float) 0);
            metrics.setJobDesc(controlDevice);
            fmcMetricsService.save(metrics);
        }else{
            metrics.setJobDesc(controlDevice);
            fmcMetricsService.updateById(metrics);
        }
        cumulativeCycleHandler(controlDevice, cycleArr);
    }

    private void cumulativeCycleHandler(String controlDevice, String[] cycle) {
        DecisionTime decisionTime = decisionTimeService.getOne(new QueryWrapper<DecisionTime>());
        String adminAddresses = xxlJobConfig.getAdminAddresses();
        XxlJobUtil xxlJobUtil = new XxlJobUtil();

        //执行器，任务Handler名称
        XxlJobInfo jobInfo = new XxlJobInfo();
        String handler = "createCycleCheckHandler";
        String jobDesc = Arrays.toString(cycle) + controlDevice + "设备将执行灌溉";
        jobInfo.setJobDesc(jobDesc);
        jobInfo.setAuthor("admin");
        jobInfo.setExecutorHandler(handler);
        jobInfo.setExecutorParam(controlDevice);
        jobInfo.setScheduleType("CRON");
        jobInfo.setScheduleConf(CronExpressionConverter.convertToCron(cycle,decisionTime.getDecisionTime()));
        jobInfo.setGlueType("BEAN");
        jobInfo.setExecutorRouteStrategy("FIRST");
        jobInfo.setMisfireStrategy("DO_NOTHING");
        jobInfo.setExecutorBlockStrategy("SERIAL_EXECUTION");
        //创建定时任务提醒用户
        xxlJobUtil.addAndStartTask("xxl-job-irrigation", adminAddresses, jobInfo);
    }

}